var express = require('express');
var router = express.Router();
var Userinformation = require('../models/userinformation');

/* GET users listing. */
// router.get('/list', function(req, res, next) {
//
//     Userinformation.find({'isdelete':'0'}).sort({'_id':-1}).exec(function (err,list) {
//       res.send(JSON.stringify(list));
//   });
//
// });


/* post users listing. */
router.post('/list', function(req, res, next) {

    var count=0;
    var page=req.body.page;
    var rows=req.body.rows;

    var username=req.body.username;
    console.log(username);
    console.log("page:"+page+",rows:"+rows);

    var query=Userinformation.find({});
    query.skip((page-1)*rows);
    query.limit(rows*1);
    if(username){
        var qs=new RegExp(username);
        query.where('username',qs);
    }
    query.where('isdelete','0');

    //计算分页数据
    query.exec(function(err,rs){
        if(err){
            var dataError = {
                status: '0',
                msg: err.message
            };
            res.end(JSON.stringify(dataError));


        }else{
            var qs=new RegExp(username);
            Userinformation.find({username:qs,isdelete:'0'}).sort({'_id':-1}).exec(function (err,list) {

                var sumPage = ((list.length%rows)==0)?(list.length/rows):(Math.floor(list.length/rows)+1);

                if(err){
                    var dataError = {
                        status: '0',
                        msg: err.message
                    };
                    res.end(JSON.stringify(dataError));

                }
                else {
                    var dataSuccess = {
                        status: '200',
                        data:rs,
                        sumpage:sumPage,
                        msg: '查询成功'
                    };
                    res.end(JSON.stringify(dataSuccess));
                }

            });

        }
    });

});




//删除用户
router.post('/delete',function(req,res,next) {
    var body = req.body;
    console.log(body);
    if(body){
        var userid  = body.userid;
        Userinformation.findOne({_id:userid},function (err,userinfo) {
            if(err){
                var dataError = {
                    status: '0',
                    msg: err.message
                };
                res.end(JSON.stringify(dataError));
            }
            else if(userinfo){


                var whereStr = {"_id":userid};
                var updateStr = {$set: { "isdelete" : '1' }};

                Userinformation.update(whereStr,updateStr,function () {
                    if(err){
                        var dataError = {
                            status: '0',
                            msg: err.message
                        };
                        res.end(JSON.stringify(dataError));
                    }
                    else {
                        var dataSuccess = {
                            status: '200',
                            msg: '删除成功'
                        };
                        res.end(JSON.stringify(dataSuccess));
                    }

                });


            }
            else {
                var dataError = {
                    status: '0',
                    msg: '用户不存在'
                };
                res.end(JSON.stringify(dataError));
            }
        });


    }


})


module.exports = router;
